<?php

namespace Apps\AppApi\Method\Search;


use AliYun\OpenSearch;
use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Enum\SearchDataType;
use Apps\Common\Libs\PicUrl;

class BaseSearch extends BaseMethod
{
    /**
     * @var \AliYun\OpenSearch
     */
    private $searchObj = null;

    /**
     * 获取搜索对象
     * @return OpenSearch
     */
    public function getSearchObj()
    {
        if($this->searchObj != null) {
            return $this->searchObj;
        }
        $config = $this->getDi()->get('appConfig')->aliyun->open_search;
        $this->searchObj = new OpenSearch($config->app_name, $config->access_key, $config->secret_key, $config->key_type, $config->host);
        return $this->searchObj;
    }

    /**
     * 获取搜索的结果数据
     * @param $result
     * @return array
     */
    public function getSearchResult($result)
    {
        if($result['status'] != 'OK') {
            return [];
        }
        if(!isset($result['result']) || !isset($result['result']['items'])) {
            return [];
        }
        $res = [];
        foreach($result['result']['items'] as $data) {
            if(isset($data['show_data'])) {
                $res[] = json_decode($data['show_data'], true);
            }
        }
        return $res;
    }

    public function searchActivity($cityId, $keyword, $limit=5, $offset=0)
    {
        $searchObj = $this->getSearchObj();
        $searchResult = $searchObj->search($keyword, ['type=' . SearchDataType::Activity, 'tag_int=' . $cityId], $limit, $offset);
        $activity = $this->getSearchResult($searchResult);
        $activity = $activity ? $activity : [];

        for($i = 0; $i < count($activity); $i++) {
            $activity[$i]['cover'] = PicUrl::ActivityCover($activity[$i]['cover'], $this->getDi());
        }

        return $activity;
    }

    public function searchBP($keyword, $limit=5, $offset=0)
    {
        $searchObj = $this->getSearchObj();
        $searchResult = $searchObj->search($keyword, 'type=' . SearchDataType::BeautyParlors, $limit, $offset);
        $bps = $this->getSearchResult($searchResult);

        $bps = $bps ? $bps : [];
        $data = [];
        foreach($bps as $bp) {

            $bp['cover'] = PicUrl::BeautyParlorCover($bp['cover'], $this->getDi());

            if(!isset($bp['tags'])) {
                $bp['tags'] = $this->getBeautyParlorTags($bp['id']);
            }
            $data[] = $bp;
        }
        return $data;
    }

    public function searchBPS($keyword, $limit=5, $offset=0)
    {
        $searchObj = $this->getSearchObj();
        $searchResult = $searchObj->search($keyword, 'type=' . SearchDataType::BeautyParlorService, $limit, $offset);
        $service = $this->getSearchResult($searchResult);
        $service = $service ? $service : [];
        $data = [];
        foreach($service as $d) {
            $d['cover'] = PicUrl::BeautyParlorCover($d['cover'], $this->getDi());
            $unit = $d['unit'];
            unset($d['unit']);
            $d['price_show'] = '';
            if($d['price'] > 0) {
                $d['price_show'] = $d['price'] . '元' . ($unit ? '/' . $unit : '');
            }
            $data[] = $d;
        }
        return $data;
    }

    public function searchQuestion($keyword, $limit=5, $offset=0)
    {
        $searchObj = $this->getSearchObj();
        $searchResult = $searchObj->search($keyword, 'type=' . SearchDataType::Question, $limit, $offset);

        $questions = $this->getSearchResult($searchResult);

        $questions = $questions ? $questions : [];
        $data = [];
        foreach($questions as $arr) {
            $arr['question_pics'] = PicUrl::Question(json_decode($arr['question_pics']), $this->getDi());
            $arr['user_cover'] = PicUrl::UserCover($arr['user_cover'], $this->getDi());
            if(unserialize(base64_decode($arr['question_content']))){
                $arr['question_content'] = unserialize(base64_decode($arr['question_content']));
            }
            $data[] = $arr;
        }
        return $data;
    }
}